package c_observer.c_java_observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by wug on 2016/6/17 0017.
 */
public class ObserverStatistics implements Observer {
    private List<Integer> speedList = new ArrayList<>();
    private List<Integer> remainTimeList = new ArrayList<>();

    @Override
    public void update(Observable observable, Object arg) {
        if (observable instanceof SubjectCar) {
            SubjectCar subjectCar = (SubjectCar)observable;
            speedList.add(subjectCar.getSpeed());
            remainTimeList.add(subjectCar.getRemainTime());
            double driveLen = 0;
            double driveTotal = 0;
            for (int i = 0; i < speedList.size(); i++) {
                driveLen += (double)speedList.get(i) * remainTimeList.get(i)/3600;
                driveTotal += (double)remainTimeList.get(i)/3600;
            }
            System.out.println("平均速度=" + driveLen/driveTotal + "km/h");
        }
    }
}
